package com.xframe.xdal.core.handler.valueTake;

import com.xframe.xdal.core.constant.DataSourceTypeEnums;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.param.*;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述：取值策略工厂
 * 创建人：xufeng
 * 创建时间：2023/03/10
 *
 * @version 1.0
 **/
public class ValueTakingStrategyFactory {

    private static Map<Class<?>, IValueTakingStrategy> map = new HashMap<>();

    static IValueTakingStrategy defValueTakingStrategy = new ValueTakingStrategy();
    static {
        map.put(String.class,new StrValueTakingStrategy());
        map.put(Long.class,new LongValueTakingStrategy());
        map.put(Integer.class,new IntValueTakingStrategy());
        map.put(Double.class,new DoubleValueTakingStrategy());
        map.put(Float.class,new FloatValueTakingStrategy());
        map.put(BigDecimal.class,new BigDecimalValueTakingStrategy());
        map.put(Date.class,new DateTakingStrategy());
        map.put(InputStream.class,new BlobValueTakingStrategy());
    }

    public static IValueTakingStrategy create(IColumnMapping columnMapping, DataSourceTypeEnums dataSourceTypeEnums){
        Class<?> dataType = columnMapping.getDataType();
        IValueTakingStrategy valueTakingStrategy = map.get(dataType);
        if(valueTakingStrategy != null) {
            return valueTakingStrategy;
        }
        if(dataType.isAssignableFrom(InputStream.class)){
            map.put(dataType,new BlobValueTakingStrategy());
            return map.get(dataType);
        }
        if(dataType.isEnum()){
            map.put(dataType,new EnumValueTakingStrategy(dataType));
            return map.get(dataType);
        }
        return defValueTakingStrategy;
    }
}
